import request, { baseURL } from '@/utils/request'
import axios from 'axios'

/**
* 登录
* @param {String} mobile - 手机号
* @param {String} code - 验证码
* @returns Promise
*/
export const userLogin = ({ mobile, code }) => {
  return request({
    url: 'v1_0/authorizations',
    method: 'post',
    data: { mobile, code }
  })
}
/**
 * 发送短信验证码
 * @returns
 */
export const sendCode = (mobile) => {
  return request({
    url: `/v1_0/sms/codes/${mobile}`
  })
}
/**
* 关注 和 取消关注
* @param {*} authorId - 作者ID
* @param {*} isFollow - 是否关注
* @returns Promise
*/
export const followAuthor = (authorId, isFollow) => {
  // 关注
  if (isFollow) {
    return request({
      url: '/v1_0/user/followings',
      method: 'post',
      data: { target: authorId }
    })
  // 取消关注
  } else {
    return request({
      url: '/v1_0/user/followings/' + authorId,
      method: 'delete'
    })
  }
}
/**
 *
 * @returns 用户个人
 */
export const getUserInfo = () => {
  return request({
    url: '/v1_0/user'
  })
}
/**
 *
 * @returns 用户信息回显
 */
export const getUserProfile = () => {
  return request({
    url: '/v1_0/user/profile'
  })
}
/**
 *
 * @param {*} userId  用户ID
 * @param {*} url 头像地址
 * @returns
 */
export const iseditUserPhoto = (data) => {
  return request({
    method: 'patch',
    url: '/v1_0/user/photo',
    data
  })
}

export const iseditUserInfo = (data) => {
  return request({
    method: 'patch',
    url: '/v1_0/user/profile',
    data
  })
}

/**
 * 刷新token
 * @param {String} refreshToken - 保存的refresh_token
 * @returns
 */
export const refreshToken = (refreshToken) => {
  // 使用默认的axios实例，不使用request发，可以绕过request实例的拦截器
  return axios({
    // 复用@/utils/request.js中的baseURL
    url: baseURL + 'v1_0/authorizations',
    method: 'put',
    headers: {
      Authorization: `Bearer ${refreshToken}`
    }
  })
}
